package cn.mr;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class NewsDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(NewsDriver.class);
        // 设置 Mapper 和 Reducer 类
        job.setMapperClass(NewsMapper.class);
        job.setReducerClass(NewsReducer.class);
        // 设置 Mapper 输出类型
        job.setMapOutputKeyClass(Text.class);    // 类别
        job.setMapOutputValueClass(NewsBean.class);  // 新闻对象
//        job.setNumReduceTasks(5);
//        job.setPartitionerClass(NewsPartitioner.class);
        // 设置最终输出类型
        job.setOutputKeyClass(Text.class);      // 类别
        job.setOutputValueClass(Text.class);    // 统计结果
        // 设置输入输出路径
        FileInputFormat.addInputPath(job, new Path("D:\\class\\20250608\\input\\realtime_hot.csv"));
        Path outputPath = new Path("D:\\class\\20250608\\outcome");
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(outputPath)) {
            fs.delete(outputPath, true);
        }
        FileOutputFormat.setOutputPath(job, outputPath);
        // 执行作业并获取结果
        boolean success = job.waitForCompletion(true);
        System.out.println(success ? "作业成功" : "作业失败");
    }
}

